package com.housediyadmin.view.titlewindow
{   	
	import com.housediyadmin.events.brandEvents.GetBrandEvent;
	import com.housediyadmin.events.goodsEvents.AddGoodsEvent;
	import com.housediyadmin.events.goodsEvents.DeleteGoodsFittingEvent;
	import com.housediyadmin.model.BrandModel;
	import com.housediyadmin.model.FittingModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.PicModel;
	import com.housediyadmin.view.components.PopUploadView;
	import com.housediyadmin.vo.Brand;
	import com.housediyadmin.vo.Fitting;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.DragSource;
	import mx.core.IUIComponent;
	import mx.events.CloseEvent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Label;
	import spark.events.IndexChangeEvent;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/28
	 */ 
	public class FittingBrandEditMediator extends Mediator
	{
		[Inject]
		public var view:FittingBrandEditView;
		
		[Inject]
		public var fittingModel:FittingModel;
		
		[Inject]
		public var brandModel:BrandModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Inject]
		public var picModel:PicModel;//writen by liaoruohuai
		
		public var popupload:PopUploadView; //writen by liaoruohuai
        
		public var fittingIDs:Array = new Array();
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.all, "dataProvider", fittingModel, "fittingsArray");
			BindingUtils.bindProperty(view.selected, "dataProvider", fittingModel, "selectedFittings");
			
			//Brand
			BindingUtils.bindProperty(view.brandDropList, "dataProvider", brandModel, "brandArray");			
			dispatch(new GetBrandEvent(GetBrandEvent.GET_ALL_BRANDS));
			view.brandDropList.addEventListener(IndexChangeEvent.CHANGE, selectBrandHandler);
			view.previewBtn.addEventListener(MouseEvent.CLICK, prePopUpHandler);
		    view.nextBtn.addEventListener(MouseEvent.CLICK, nextPopUpHandler);
			//view.nextBtn.addEventListener(MouseEvent.CLICK, popUpHandler);//writen by liaoruohuai
			view.cancleBtn.addEventListener(MouseEvent.CLICK, closeHandler);
		    view.addEventListener(CloseEvent.CLOSE, closeHandler);
			view.all.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);	
			view.selected.addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler);		
			view.selected.addEventListener(DragEvent.DRAG_DROP, dragDropHandler);
			view.selected.addEventListener(DeleteGoodsFittingEvent.DELETE_FITTING,deletefittingHandler);
		}
		
		private function deletefittingHandler(event:DeleteGoodsFittingEvent):void
		{	
			for each (var fitting:Fitting in fittingModel.selectedFittings)
			{
				if (fitting.fittingID == event.fittingID)
				{
					var index:int = fittingModel.selectedFittings.getItemIndex(fitting);	
					fittingModel.selectedFittings.removeItemAt(index);
				}
			}
		}
		
		private function startDragHandler(event:MouseEvent):void
		{	
			if (view.all.selectedItem != null)
			{
				var dragSource:DragSource = new DragSource();
				dragSource.addData(view.all.selectedItem, "moveFits");
				
				var imageProxy:Label = new Label();
				imageProxy.text = view.all.selectedItem.fittingName;
				
				DragManager.doDrag(view.all, dragSource, event, imageProxy, 20 - view.all.mouseX, 20 - view.all.mouseY);	
			}
		}
		
		private function dragEnterHandler(event:DragEvent):void
		{
			var couldDrop:Boolean = true;
			var selectedFit:Fitting = event.dragSource.dataForFormat(event.dragSource.formats[0]) as Fitting;	
			for each (var fit:Fitting in fittingModel.selectedFittings)
			{
				if (fit == selectedFit)
				{
					couldDrop = false;
					break;		
				}
			}
			if (couldDrop == true && event.dragSource.hasFormat("moveFits"))
			{
				DragManager.acceptDragDrop(IUIComponent(event.target));
			}		
		}
		
		private function dragDropHandler(event:DragEvent):void
		{
			fittingModel.selectedFittings.addItem(event.dragSource.dataForFormat(event.dragSource.formats[0]));	
			fittingIDs.push((event.dragSource.dataForFormat(event.dragSource.formats[0]) as Fitting).fittingID);
		}
		
		private function prePopUpHandler(event:MouseEvent):void
		{
			fittingModel.selectedFittings.removeAll();
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
			var popUp:EditGoodsView = new EditGoodsView();
			PopUpManager.addPopUp(popUp, view, true);
			mediatorMap.createMediator(popUp);
			PopUpManager.centerPopUp(popUp);
		}
		
		private function nextPopUpHandler(event:MouseEvent):void
		{
			if(view.brandDropList.selectedItem == null)
			{
				Alert.show("请选择品牌信息");
			}
			else
			{
				dispatch(new AddGoodsEvent(AddGoodsEvent.ADD_GOODS, goodsModel.addGoods, fittingIDs)); 
				PopUpManager.removePopUp(view);
				mediatorMap.removeMediator( this );
				var popUp:UploadGoodsPicView = new UploadGoodsPicView();
				PopUpManager.addPopUp(popUp, view, true);
				mediatorMap.createMediator(popUp);
				PopUpManager.centerPopUp(popUp);
			}			
		}
		
		private function closeHandler(event:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this);
		}
		
		//writen by liaoruohuai
		private function popUpHandler(event:MouseEvent):void
		{			
			/*popupload = new PopUploadView();
			PopUpManager.addPopUp(popupload, view, true);
			PopUpManager.centerPopUp(popupload);
			mediatorMap.createMediator(popupload);
			popupload.addEventListener(CloseEvent.CLOSE,close);*/
		}
		//writen by liaoruohuai
		private function close(e:Event):void
		{
			PopUpManager.removePopUp(popupload);
			mediatorMap.removeMediator( this );	
			Alert.show(picModel.currentFiles.length.toString());
			nextPopUpHandler(e as  MouseEvent);
		}
		
		private function selectBrandHandler(event:IndexChangeEvent):void
		{
			goodsModel.addGoods.brandID = (view.brandDropList.selectedItem as Brand).brandID;
		}
	}
}